package com.go.ssh.point.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.util.StringUtils;

import com.go.ssh.commons.Constants;
import com.go.ssh.commons.pagination.Page;
import com.go.ssh.commons.pagination.PageUtil;
import com.go.ssh.commons.service.impl.BaseServiceImpl;
import com.go.ssh.point.dao.IGoodsCodeDao;
import com.go.ssh.point.exception.NotCodeException;
import com.go.ssh.point.model.GoodsCodeModel;
import com.go.ssh.point.model.GoodsModel;
import com.go.ssh.point.service.IGoodsCodeService;
import com.go.ssh.point.service.IGoodsService;

public class GoodsCodeServiceImpl extends
		BaseServiceImpl<GoodsCodeModel, Integer> implements IGoodsCodeService {

	private IGoodsService goodsService;

	@Override
	public Page<GoodsCodeModel> listAllByGoods(int pn, int goodsId) {
		Integer count = getGoodsCodeDao().countAllByGoods(goodsId);
		List<GoodsCodeModel> items = getGoodsCodeDao().listAllByGoods(pn,
				goodsId);
		return PageUtil.getPage(count, pn, items, Constants.DEFAULT_PAGE_SIZE);
	}

	private IGoodsCodeDao getGoodsCodeDao() {
		return (IGoodsCodeDao) getDao();
	}

	@Override
	public void save(int goodsId, String[] codes) {
		GoodsModel goods = goodsService.get(goodsId);
		for (String code : codes) {
			if (StringUtils.hasText(code)) {
				GoodsCodeModel goodsCode = new GoodsCodeModel();
				goodsCode.setCode(code);
				goodsCode.setGoods(goods);
				save(goodsCode);
			}
		}
	}

	@Override
	public GoodsCodeModel buy(String username, int goodsId)
			throws NotCodeException {
		// 1、验证用户积分是否充足
		// 2、其他逻辑判断
		// 3、记录交易记录开始
		GoodsCodeModel goodsCode = getGoodsCodeDao()
				.getOneNotExchanged(goodsId);
		if (goodsCode == null) {
			// 3、记录交易记录失败
			throw new NotCodeException();
		}
		goodsCode.setExchanged(true);
		goodsCode.setExchangeTime(new Date());
		goodsCode.setUsername(username);
		save(goodsCode);
		// 3、记录交易记录成功
		return goodsCode;
	}

	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}
}
